<template>
  <pro-layout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    v-bind="settings"
  >
    <template v-slot:menuHeaderRender>
      <div @click="$nav('/')">
        <img :src="logo">
        <h1>{{ title }}</h1>
      </div>
    </template>
    <setting-drawer
      v-if="isDev"
      :settings="settings"
      @change="handleSettingChange"
    >
      <div style="margin: 12px 0;">
        This is SettingDrawer custom footer content.
      </div>
    </setting-drawer>
    <template v-slot:rightContentRender>
      <right-content
        :top-menu="settings.layout === 'topmenu'"
        :is-mobile="isMobile"
        :theme="settings.theme"
        color="#555"
      />
    </template>
    <router-view />
  </pro-layout>
</template>

<script>
import { SettingDrawer, updateTheme } from "@ant-design-vue/pro-layout";
import { i18nRender } from "@/locales";
import { mapState } from "vuex";
import {
  CONTENT_WIDTH_TYPE,
  SIDEBAR_TYPE,
  TOGGLE_MOBILE_TYPE,
} from "@/store/mutation-types";

import defaultSettings from "@/config/defaultSettings";
import RightContent from "@/components/GlobalHeader/RightContentAdmin";
import GlobalFooter from "@/components/GlobalFooter";

import { asyncRouterMap } from "@/config/router.config.js"; //免登陆

export default {
  name: "BasicLayout",
  components: {
    SettingDrawer,
    RightContent,
    GlobalFooter,
  },
  data() {
    return {
      logo: require("@/assets/logo.svg"),
      // preview.pro.antdv.com only use.
      isProPreviewSite:
        process.env.VUE_APP_PREVIEW === "true" &&
        process.env.NODE_ENV !== "development",
      // end
      isDev: process.env.NODE_ENV === "development",

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        // layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        layout: "sidemenu", // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: "Fluid",
        // 主题 'dark' | 'light'
        theme: "dark",
        // 主色调
        primaryColor: "#1890ff",
        fixedHeader: true,
        fixSiderbar: true,
        colorWeak: false,

        hideHintAlert: false,
        hideCopyButton: false,
      },

      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false,
    };
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: (state) => state.asyncRouter.addRouters,
    }),
  },
  created() {
    if (defaultSettings.routerServe) {
      let routes = this.mainMenu.find((item) => item.path === "/");
      // routes = routes.children.find((item) => item.path === "/admin");
      this.menus = (routes && routes.children) || [];
      // 处理侧栏收起状态
      this.$watch("collapsed", () => {
        this.$store.commit(SIDEBAR_TYPE, this.collapsed);
      });
      this.$watch("isMobile", () => {
        this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile);
      });
    } else {
      //免登陆
      let routes = asyncRouterMap.find((item) => item.path === "/");
      routes = routes.children.find((item) => item.path === "/admin");
      this.menus = (routes && routes.children) || [];
    }
    // console.log("菜单---",this.menus);
  },
  // created () {
  //     //免登陆
  //   this.menus = asyncRouterMap.find((item) => item.path === '/').children
  //   console.log('菜单----',this.menus)
  //   this.collapsed = !this.sidebarOpened
  // },

  mounted() {
    const userAgent = navigator.userAgent;
    if (userAgent.indexOf("Edge") > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed;
        setTimeout(() => {
          this.collapsed = !this.collapsed;
        }, 16);
      });
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (
      process.env.NODE_ENV !== "production" ||
      process.env.VUE_APP_PREVIEW === "true"
    ) {
      updateTheme(this.settings.primaryColor);
    }
  },
  methods: {
    i18nRender,
    handleMediaQuery(val) {
      this.query = val;
      if (this.isMobile && !val["screen-xs"]) {
        this.isMobile = false;
        return;
      }
      if (!this.isMobile && val["screen-xs"]) {
        this.isMobile = true;
        this.collapsed = false;
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid;
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse(val) {
      this.collapsed = val;
    },
    handleSettingChange({ type, value }) {
      // console.log("type", type, value);
      type && (this.settings[type] = value);
      switch (type) {
        case "contentWidth":
          this.settings[type] = value;
          break;
        case "layout":
          if (value === "sidemenu") {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid;
          } else {
            this.settings.fixSiderbar = false;
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed;
          }
          break;
      }
    },
  },
};
</script>

<style lang="less">
@import "./BasicLayout.less";
</style>
